<?php
	include("dboper.php");

	if(!@$_POST['akses'] == true) {
	   die('Akses langsung tidak diperbolehkan!');
	}

    $d_sistem = getConfig();
	$h_vote = getAllVoteJaprak($d_sistem['japrak']);
    $d_count = getCountPCJaprak($d_sistem['japrak']);
    echo '<h4>Daftar Vote (Total PC = '.$d_count['TOTAL'].')</h4>';
	$d_japrak=getDetailJaprak($d_sistem['japrak']);
	//$d_asisten=getNgasistenJaprak(
?>


<br>
<button id="simpan" >&#9658; Download</button> <span style="font:10px Tahoma;">*data pesan bisa dilihat didalam file(download)</span>
<br><br><div id="dataVote">
<table>
    <tr><td>No</td><td>IP Voter</td><td style='display:none;'>NIM</td><td>Asisten</td><td>Nilai</td><td>Matakuliah</td><td>Kelas</td><td>Keterangan</td><td style='display:none;'>Pesan</td></tr>
    <?php 
    $i = 1;
    $pesan = '';
    while ($d_vote = mysql_fetch_array($h_vote)) { 
        
         echo "<tr><td>".$i."</td><td>".$d_vote['ip_voter']."</td><td style='display:none;'>".$d_vote['nim_asisten']."</td><td>".$d_vote['nama_asisten']."</td><td>".$d_vote['nilai']."</td><td>".$d_japrak['nama_matkul']."</td><td>".$d_japrak['nama_kelas']."</td><td>".$d_vote['det']."</td><td style='display:none;'>".$d_vote['kesan_pesan']."</td></tr>";
        $i++;
    } ?>
</table></div>
<link rel="stylesheet" href="pretty.css" type="text/css" media="screen" charset="utf-8" />
<script src="pretty.js" type="text/javascript" charset="utf-8"></script>
<script>

        
    function browserTester(browserString) {
		return navigator.userAgent.toLowerCase().indexOf(browserString) > -1;
	}
    
    function browserTester(browserString) {
		return navigator.userAgent.toLowerCase().indexOf(browserString) > -1;
	}
    
    $("#simpan").click(function(e){
        var a = document.createElement('a');
        //getting data from our div that contains the HTML table
        var data_type = 'data:application/vnd.ms-excel;';
        var makul = '<?php echo $d_japrak["nama_matkul"]; ?>';
        var kelas = '<?php echo $d_japrak["nama_kelas"]; ?>';
        var table_div = document.getElementById('dataVote');
		var uri = 'data:application/vnd.ms-excel;base64,'
		, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>Matakuliah : '+makul+' | Kelas : '+kelas+'<table>{table}</table></body></html>'
		, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
		, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }

        var table_html = table_div.outerHTML.replace(/<\/?a(|\s+[^>]+)>/g, '');
				
		var d = new Date();
		var curr_date = d.getDate();
		var curr_month = d.getMonth() + 1; //Months are zero based
		var curr_year = d.getFullYear();
		var tanggal = curr_date + "-" + curr_month + "-" + curr_year;
		
		var ctx = {worksheet: tanggal || 'Worksheet', table: table_html}


		if(browserTester('chrome')) {
		
			a.href = uri + base64(format(template, ctx));
			//setting the file name
			a.download = 'dataVote'+tanggal+'.xls';
			//triggering the function
			a.click();
			//just in case, prevent default behaviour	
		} else if(browserTester('mozilla')) {
			window.location.href = uri + base64(format(template, ctx))
		}

        e.preventDefault();

    });

</script>

